#include "logo.h"

extern  double evaluation(char*);

char* getValue(void)
{

        char *fucName[]=
             {"THING","READLIST","AND","OR","NOT","RANDOM",
              "REMAINDER","SQRT","INT","NUMBERP","WORDP","LISTP","EMPTYP",
              "EQUALP","NAMEP","WORD","SENTENCE","FIRST","LAST","BUTFIRST",
              "BUTLAST",};
        int fucNumber= sizeof(fucName)/sizeof(char*);
        char* (*fucP[])(void)=
             {thing,readlist,and,or,not,random,fucRemainder,fucSqrt,fucInt,
              numberp,wordp,listp,emptyp,equalp,namep,fucWord,sentence,
              first,last,butfirst,butlast};//
        char strvalue[MAXVALUE]="\0";
        char str[MAXVALUE]="\0",
             strfuc[MAXVALUE]="\0",
             strword[MAXVALUE]="\0",
             strfunc[MAXVALUE]="\0",
             strfunans[MAXVALUE]="\0",
             strvalue1[MAXVALUE] ="\0",
             strvalue2[MAXVALUE] ="\0",
             string[MAXVALUE] ="\0";
        char *value;                   //answer
        char *fuc;                     //input
        char *func;                    //function name
        char *value1 =strvalue1;
        char *value2 =strvalue2;
        char *fucAns;
        char *wordN;                   //name of word
        char nextChar;          //judgement of whther to go on analyse
        int i;                         //loop var
        int j;                         //loop var
        int l;
        int k;
        int fuclen;
        int calP;              //whther cal

        printf("in getValue\n");
        calP=0;
        value = strvalue;
        fuc = strfuc;
        wordN=strword;
        func=strfunc;
        fucAns=strfunans;
        nextChar =getcharInput();
        while (isspace(nextChar))
        {
              nextChar =getcharInput();
              printf("nextChar is %c\n",nextChar);
        }
        while (!isspace(nextChar))
        {
                putcharInput(nextChar);
                fuc =getInput();
                
                if (fuc[0] =='[')
                {
                   strcpy(string,fuc);
                   putInput(string);
                   return readlist();
                }
                sscanf(fuc,"%[^]]",fuc);
                //printf("fuc is %s\n",fuc);
                //printf("fuclen is %d\n",strlen(fuc));
                fuclen=strlen(fuc);
                for (i=0;i< fuclen;i++)                               //analyse the input
                {
                    //printf("i= %d\n",i);
                    //printf("fuc is %s\n",fuc);
                    //printf("fuclen is %d\n",strlen(fuc));
                    //printf("fuc[%d]= %c\n",i,fuc[i]);
                    if (fuc[i] == ':')//((strchr(fuc+i,':')) == fuc+i)
                    {
                       sscanf(fuc+i+1,"%[1-9,a-z,A-Z]",wordN);               //get value of :
                       fucAns=findWord(wordN);
                       strcat(value,fucAns);
                       printf(":wordN is %s\n",wordN);
                       printf(":findword is %s\n",wordN);
                       i= i+strlen(wordN);
                    }else if (fuc[i] == '"')
                    {
                          sscanf(fuc+i+1,"%[0-9,A-Z,a-z]",wordN);               //get value of :
                          strcat(value,wordN);
  //                        printf("\"wordN is %s\n",wordN);
                          i= i+strlen(wordN);
                    }else if (isalpha(fuc[i]))                                 //get value of function
                    {
                       printf("get value of function\n");
                       sscanf(fuc+i,"%[A-Z]",func);
                       printf("func is %s\n",func);
                       for (j=0;j<fucNumber;j++)
                       {
                           //printf("fucname is %s\n",fucName[j]);
                           if (!strcmp(func,fucName[j]))
                           {
                              //printf("get value of %s\n",fucName[j]);
                              fucAns =(*fucP[j])();
                              strcat(value,fucAns);
                              printf("get value of %s is %s\n",fucName[j],value);
                              goto conwhile;
                           }
                       }
                       for (j=0;j<toLen;j++)             //find to
                       {
                           if (!strcmp(func,toName[j][0]))
                           {
                              putInput(func);
                              strcpy(fucAns,runTo());
                              strcat(value,fucAns);
                              goto conwhile;
                           }
                       }
                       if (i+strlen(func) <strlen(fuc))
                       {
                          printf("want to put %s\n",fuc+i+strlen(func));
                          strcpy(string,fuc+i+strlen(func));
                          printf("want to put %s\n",string);
                          putInput(string);
                       }
                       return func;
                    }else if ((isdigit(fuc[i])) || (op(fuc[i])) || (fuc[i] =='.'))                 //get number & op
                    {
                         calP = 1;
                         l =strlen(value);
                         value[l]=fuc[i];
                         value[l+1]='\0';
                    }/*else if (fuc[i] =='[')
                    {
                          putInput(fuc);
                          return readlist();
                    }else
                    {
                         printf("fuc+i= %s\n",fuc+i);
                         j =i;
                         k=0;
                         printf("fuc[%d]= %c\n",j,fuc[j]);
                         if (fuc[j] =='[')
                         {
                            k++;
                            l =strlen(value);
                            value[l]=fuc[j];
                            value[l+1]='\0';
                            j++;
                         }
                         while ((j<fuclen) && (k))
                         {
                               while (fuc[j]=='[')
                               {
                                     k++;
                                     l =strlen(value);
                                     value[l]=fuc[j];
                                     value[l+1]='\0';
                                     printf("fuc[%d]= %c\n",j,fuc[j]);
                                     j++;
                               }
                               while (fuc[j]==']')
                               {
                                     k--;
                                     l =strlen(value);
                                     value[l]=fuc[j];
                                     value[l+1]='\0';
                                     printf("fuc[%d]= %c\n",j,fuc[j]);
                                     j++;
                               }
                               l =strlen(value);
                               value[l]=fuc[j];
                               value[l+1]='\0';
                               printf("fuc[%d]= %c\n",j,fuc[j]);
                               j++;
                         }
                         i =j;
                    }*/
                }
                conwhile:
                         nextChar=getcharInput();                              //whther go on analyse
        }
        exit:
             putcharInput(nextChar);
             if (calP)                                                    //cal or print
             {
                if (strstr(value,"="))
                {
                   sscanf(value,"%[^=]",value1);
                   value2 =value+strlen(value1)+1;
                   printf("the compare is %s and %s\n", value1,value2);
                   if (evaluation(value1) == evaluation(value2))
                      {
                                          value="TRUE";
                      }else
                      {
                           value ="FALSE";
                      }
                }else if (strstr(value,">"))
                {
                   sscanf(value,"%[^>]",value1);
                   value2 =value+strlen(value1)+1;
                   printf("the compare is %s and %s\n", value1,value2);
                   if (evaluation(value1) > evaluation(value2))
                      {
                                          value="TRUE";
                      }else
                      {
                           value ="FALSE";
                      }
                }else if (strstr(value,"<"))
                {
                   sscanf(value,"%[^<]",value1);
                   value2 =value+strlen(value1)+1;
                   printf("the compare is %s and %s\n", value1,value2);
                   if (evaluation(value1) < evaluation(value2))
                      {
                                          value="TRUE";
                      }else
                      {
                           value ="FALSE";
                      }
                }else
                {
                     printf("value of evaluation is %s\n",value);
                     sprintf(value, "%lf",evaluation(value));
                }
             }
             printf("value of getValue is %s\n",value);
             return value;
}
/*  char s[100] = "\0";
  char *value = s;
  char string[100] = "\0";
  char *word = s;
  char c;
  int isnumber;
  int isdecimal;
  int isbrackets;
  int i;
  double answer = 0;

  isdecimal = 2;
  isnumber = 1;
  isbrackets = 1;
  while ((!isspace(c = getchar())))// && (isdecimal > 0) &&
  {
    switch (c) {
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9'://isnumber = 1;
	         break;
	case '.'://isdecimal--;
	         break;
	case '+':
	case '-':
	case '*':
	case '/':if (!strcmp(string, ""))
	         {
			   strcpy(value, string);
			   goto RE;
			 }
//	         isnumber = 0;
//			 isdecimal = 2;
			 break;
	case '('://isbrackets++;
	         break;
	case ')'://isbrackets--;
	         break;
	case '\"' :
      strcpy(word, getWord());
	  if (word)
	  {
        strcat(string,findWord(word));
		continue;
	  }
	  else
	  {
// 	    printf("No value of word\n");
        value[0] = '\0';
        goto RE;
	  }
	  break;
	case ':' :
      strcpy(s, getWord());
	  strcpy(word, findWord(s));
      word[strlen(word)] = 0;
//      printf(": word is %s.\n", word);
	  if (word)
	  {
	    strcat(string,word);
        continue;
      }
	  else
	  {
	    value[0] = '\0';
		goto RE;
	  }
	  break;
    default :
             while ((c != EOF) && (!isspace(c)))
             {
//                   printf("THE char is %c\n", c);
                   i = strlen(word);
                   word[i++] = c;
                   word[i] = 0;
                   c = getchar();
             }
             if (!strcmp(word, "THING"))
			 {
               strcpy(s, getWord());
//               printf("THING %s\n", s);
               strcpy(word, findWord(s));
			   if (word)
			   {
			     strcat(string, word);
			     continue;
			   }
			   else
			   {
			     value[0] = '\0';
				 goto RE;
			   }
			 }
			 else
			 {
			   if (!strcmp(word, "READLIST"))
			   {
			     scanf("%s", &word);
			   }
			 }
             printf("%s\n", word);
			 strcat(string, word);
	         strcpy(value, string);
             goto RE;
             break; }
    i = strlen(string);
    string[i++] = c;
    string[i] = 0;
  }

  printf("%s.\n", string);

  answer = evaluation(string);
  printf("answer of evaluation:%lf\n", answer);
  if (answer)
  {
    sprintf(value, "%lf", answer);
    goto RE;
  }
  else
  {
    strcpy(value, string);
  }

  RE:
    return &value[0];
}
*/
